package pattern.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pattern.WorkServiceTool;
import pattern.annontations.CacheDel;
import pattern.annontations.Menu;
import pattern.annontations.Secure;
import pattern.cache.InitCache;
import pattern.constant.CacheKeyConstant;
import pattern.currentsession.ContextLocalCache;
import pattern.dto.RoleOfDept;
import pattern.dto.SysDept;
import pattern.dto.model.system.DeptAddOrUpdateModel;
import pattern.dto.response.system.DeptTreeResponse;
import pattern.model.BaseIdModel;
import pattern.result.ResponseData;
import pattern.routing.WorkMainApiRouting;
import pattern.utils.CollectionUtil;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author Simon
 * @date 2021-04-25
 */
@Api(tags = "部门管理")
@RestController
@RequestMapping(WorkMainApiRouting.Dept.BASE)
@Menu(menuName = "dept", menuUri = "dept", foundational = false)
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@Secure
@Slf4j
public class DeptController {

    private final InitCache initCache;

    @ApiOperation(value = "查询")
    @GetMapping(WorkMainApiRouting.Dept.LIST)
    @Secure(actionUri = "dept:search", actionName = "search", actionNote = "dept")
    public ResponseData<List<DeptTreeResponse>> search(@RequestParam(required = false) String name, @RequestParam(required = false) String status) {
        return WorkServiceTool.getSysDeptService().list(new BaseIdModel<>(ContextLocalCache.get().getDeptId()), name, status);
    }

    @ApiOperation(value = "详情")
    @GetMapping(WorkMainApiRouting.Dept.DETAIL)
    @Secure(actionUri = "dept:search")
    public ResponseData<SysDept> detail(@PathVariable Integer id) {
        return new ResponseData<>(WorkServiceTool.getSysDeptService().getById(new BaseIdModel<>(id)));
    }

    @ApiOperation(value = "角色关联的部门")
    @GetMapping(WorkMainApiRouting.Dept.ROLE_DEPT_TREE)
    @Secure(actionUri = "dept:search")
    public ResponseData roleDeptTree(@PathVariable Integer roleId) {
        Map<String, Object> result = Maps.newHashMap();
        result.put("checkedKeys", WorkServiceTool.getRoleOfDeptService().getBaseMapper()
                .selectList(new LambdaQueryWrapper<RoleOfDept>().eq(RoleOfDept::getRoleId, roleId)).stream().map(RoleOfDept::getDeptId).collect(Collectors.toList()));
        result.put("depts", WorkServiceTool.getSysDeptService().tree(new BaseIdModel<>(ContextLocalCache.get().getDeptId())));
        return new ResponseData<>(result);
    }

    @ApiOperation(value = "部门树")
    @GetMapping(WorkMainApiRouting.Dept.DEPT_TREE)
    @Secure(actionUri = "dept:search")
    public ResponseData deptTree() {
        return new ResponseData<>(WorkServiceTool.getSysDeptService().tree(new BaseIdModel<>(ContextLocalCache.get().getDeptId())));
    }

    @ApiOperation(value = "新增")
    @PostMapping
    @Secure(actionUri = "dept:add", actionName = "add", actionNote = "dept")
    @CacheDel(CacheKeyConstant.SysDept.DEPT_KEY)
    public ResponseData<Boolean> add(@Valid @RequestBody DeptAddOrUpdateModel model) {
        return new ResponseData<>(Objects.nonNull(WorkServiceTool.getSysDeptService().saveOrUpdate(model)));
    }

    @ApiOperation(value = "修改")
    @PutMapping
    @Secure(actionUri = "dept:edit", actionName = "edit", actionNote = "dept")
    @CacheDel(CacheKeyConstant.SysDept.DEPT_KEY)
    public ResponseData<Boolean> edit(@Valid @RequestBody DeptAddOrUpdateModel model) {
        //修改子项
        var flag = Objects.nonNull(WorkServiceTool.getSysDeptService().saveOrUpdate(model));
        return new ResponseData<>(flag);
    }

    @ApiOperation(value = "删除")
    @DeleteMapping(WorkMainApiRouting.Dept.DELETE)
    @Secure(actionUri = "dept:remove", actionName = "delete", actionNote = "dept")
    @CacheDel(CacheKeyConstant.SysDept.DEPT_KEY)
    public ResponseData<Boolean> delete(@PathVariable Integer[] id) {
        return new ResponseData<>(WorkServiceTool.getSysDeptService().removeByIds(new BaseIdModel<>(CollectionUtil.toList(id))));
    }

}
